package com.liuym.student.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LiuYiMing
 * @date 2018/11/7 10:15
 * @describe：dubbo配置类
 */
@Configuration
@DubboComponentScan(basePackages = "com.liuym.student.dubbo.impl")
public class DubboConfig {
    @Value("${dubbo.application.name}")
    private String applicationName;
    @Value("${zookeeper.address}")
    private String address;
    @Value("${dubbo.protocol.timeout}")
    private int timeout;
    @Value("${dubbo.protocol.name}")
    private String protocalName;
    @Value("${dubbo.protocol.port}")
    private int port;

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
        applicationConfig.setQosEnable(false);
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(address);
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        registryConfig.setCheck(false);
        return registryConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName(protocalName);
        protocolConfig.setPort(port);
        return protocolConfig;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(timeout);
        consumerConfig.setCheck(false);
        return consumerConfig;
    }
}
